package com.itheima.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper,Emp> implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private DeptService deptService;
    @Autowired
    private EmpExprService empExprService;
    @Override
    public PageResult<Emp> pageQuery(EmpQueryParam param) {
//        //queryWrapper配置
//        LambdaQueryWrapper<Emp> queryWrapper =new LambdaQueryWrapper<>();
//        queryWrapper
//                .like(StrUtil.isNotBlank(param.getName()),Emp::getName,param.getName())
//                .between(ObjectUtil.isNotNull(param.getBegin())||ObjectUtil.isNotNull(param.getEnd()),
//                        Emp::getCreateTime,param.getBegin(),param.getEnd())
//                .eq(ObjectUtil.isNotNull(param.getGender()),Emp::getGender,param.getGender());
//        //page配置
//        Page<Emp> page =Page.of(param.getPage(),param.getPageSize());
//        page.addOrder(OrderItem.asc("update_time"));
//        Page<Emp> pageResult = empMapper.selectPage(page, queryWrapper);
        Page<Emp> page = Page.of(param.getPage(), param.getPageSize());
        page.addOrder(OrderItem.asc("update_time"));
        page= lambdaQuery()
                .like(StrUtil.isNotBlank(param.getName()), Emp::getName, param.getName())
                .between(ObjectUtil.isNotNull(param.getBegin()) || ObjectUtil.isNotNull(param.getEnd()),
                        Emp::getCreateTime, param.getBegin(), param.getEnd())
                .eq(ObjectUtil.isNotNull(param.getGender()), Emp::getGender, param.getGender())
                .page(page);
        List<Emp> empList = page.getRecords();
        Set<Integer> deptIds = empList.stream().map(emp -> emp.getDeptId()).collect(Collectors.toSet());
        if (ObjectUtil.isNotNull(deptIds)){
            List<Dept> depts = deptService.listByIds(deptIds);
            Map<Integer, String> map = depts.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
            empList.forEach(emp -> {
                emp.setDeptName(map.getOrDefault(emp.getDeptId(),"？部门"));
            });
        }
        return new PageResult<>(page.getTotal(),empList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeEmps(List<Integer> ids) {
        removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId,ids));
    }

    @Override
    public Emp getEmpById(Integer id) {
        Emp emp = getById(id);
        emp.setExprList(empExprService.list(
                new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId())
        ));
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId,emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
    }

}
